﻿
namespace Groceries.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.Web.DomainServices;
    using System.Web.DomainServices.Providers;
    using System.Web.Ria;
    using System.Web.Ria.Services;
    using Groceries.Web;


    // Implements application logic using the GroceriesModelConnection context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class GroceriesDomainService : LinqToEntitiesDomainService<GroceriesModelConnection>
    {

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<Item> GetItems()
        {
            return this.ObjectContext.Items.Include("Supplier").OrderBy(e => e.Name);
        }

        public void InsertItem(Item item)
        {
            if ((item.EntityState != EntityState.Added))
            {
                if ((item.EntityState != EntityState.Detached))
                {
                    this.ObjectContext.ObjectStateManager.ChangeObjectState(item, EntityState.Added);
                }
                else
                {
                    this.ObjectContext.AddToItems(item);
                }
            }
        }

        public void UpdateItem(Item currentItem)
        {
            if ((currentItem.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentItem, this.ChangeSet.GetOriginal(currentItem));
            }
        }

        public void DeleteItem(Item item)
        {
            if ((item.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(item);
            }
            this.ObjectContext.DeleteObject(item);
        }

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<ItemsLocation> GetItemsLocations()
        {
            return this.ObjectContext.ItemsLocations;
        }

        public void InsertItemsLocation(ItemsLocation itemsLocation)
        {
            if ((itemsLocation.EntityState != EntityState.Added))
            {
                if ((itemsLocation.EntityState != EntityState.Detached))
                {
                    this.ObjectContext.ObjectStateManager.ChangeObjectState(itemsLocation, EntityState.Added);
                }
                else
                {
                    this.ObjectContext.AddToItemsLocations(itemsLocation);
                }
            }
        }

        public void UpdateItemsLocation(ItemsLocation currentItemsLocation)
        {
            if ((currentItemsLocation.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentItemsLocation, this.ChangeSet.GetOriginal(currentItemsLocation));
            }
        }

        public void DeleteItemsLocation(ItemsLocation itemsLocation)
        {
            if ((itemsLocation.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(itemsLocation);
            }
            this.ObjectContext.DeleteObject(itemsLocation);
        }

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<Location> GetLocations()
        {
            return this.ObjectContext.Locations;
        }

        public void InsertLocation(Location location)
        {
            if ((location.EntityState != EntityState.Added))
            {
                if ((location.EntityState != EntityState.Detached))
                {
                    this.ObjectContext.ObjectStateManager.ChangeObjectState(location, EntityState.Added);
                }
                else
                {
                    this.ObjectContext.AddToLocations(location);
                }
            }
        }

        public void UpdateLocation(Location currentLocation)
        {
            if ((currentLocation.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentLocation, this.ChangeSet.GetOriginal(currentLocation));
            }
        }

        public void DeleteLocation(Location location)
        {
            if ((location.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(location);
            }
            this.ObjectContext.DeleteObject(location);
        }

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<Recipe> GetRecipes()
        {
            return this.ObjectContext.Recipes;
        }

        public void InsertRecipe(Recipe recipe)
        {
            if ((recipe.EntityState != EntityState.Added))
            {
                if ((recipe.EntityState != EntityState.Detached))
                {
                    this.ObjectContext.ObjectStateManager.ChangeObjectState(recipe, EntityState.Added);
                }
                else
                {
                    this.ObjectContext.AddToRecipes(recipe);
                }
            }
        }

        public void UpdateRecipe(Recipe currentRecipe)
        {
            if ((currentRecipe.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentRecipe, this.ChangeSet.GetOriginal(currentRecipe));
            }
        }

        public void DeleteRecipe(Recipe recipe)
        {
            if ((recipe.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(recipe);
            }
            this.ObjectContext.DeleteObject(recipe);
        }

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<RecipesItem> GetRecipesItems()
        {
            return this.ObjectContext.RecipesItems;
        }

        public void InsertRecipesItem(RecipesItem recipesItem)
        {
            if ((recipesItem.EntityState != EntityState.Added))
            {
                if ((recipesItem.EntityState != EntityState.Detached))
                {
                    this.ObjectContext.ObjectStateManager.ChangeObjectState(recipesItem, EntityState.Added);
                }
                else
                {
                    this.ObjectContext.AddToRecipesItems(recipesItem);
                }
            }
        }

        public void UpdateRecipesItem(RecipesItem currentRecipesItem)
        {
            if ((currentRecipesItem.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentRecipesItem, this.ChangeSet.GetOriginal(currentRecipesItem));
            }
        }

        public void DeleteRecipesItem(RecipesItem recipesItem)
        {
            if ((recipesItem.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(recipesItem);
            }
            this.ObjectContext.DeleteObject(recipesItem);
        }

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<ShoppingList> GetShoppingLists()
        {
            return this.ObjectContext.ShoppingLists.Include("ShoppingListsItems").OrderBy(e => e.Name);
        }

        public void InsertShoppingList(ShoppingList shoppingList)
        {
            if ((shoppingList.EntityState != EntityState.Added))
            {
                if ((shoppingList.EntityState != EntityState.Detached))
                {
                    this.ObjectContext.ObjectStateManager.ChangeObjectState(shoppingList, EntityState.Added);
                }
                else
                {
                    shoppingList.ID = Guid.NewGuid();
                    shoppingList.DateModified = DateTime.Now;
                    this.ObjectContext.AddToShoppingLists(shoppingList);
                }
            }
        }

        public void UpdateShoppingList(ShoppingList currentShoppingList)
        {
            if ((currentShoppingList.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentShoppingList, this.ChangeSet.GetOriginal(currentShoppingList));
            }
        }

        public void DeleteShoppingList(ShoppingList shoppingList)
        {
            if ((shoppingList.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(shoppingList);
            }
            this.ObjectContext.DeleteObject(shoppingList);
        }

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<ShoppingListsItem> GetShoppingListsItems()
        {
            return this.ObjectContext.ShoppingListsItems;
        }

        public void InsertShoppingListsItem(ShoppingListsItem shoppingListsItem)
        {
            if ((shoppingListsItem.EntityState != EntityState.Added))
            {
                if ((shoppingListsItem.EntityState != EntityState.Detached))
                {
                    this.ObjectContext.ObjectStateManager.ChangeObjectState(shoppingListsItem, EntityState.Added);
                }
                else
                {
                    this.ObjectContext.AddToShoppingListsItems(shoppingListsItem);
                }
            }
        }

        public void UpdateShoppingListsItem(ShoppingListsItem currentShoppingListsItem)
        {
            if ((currentShoppingListsItem.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentShoppingListsItem, this.ChangeSet.GetOriginal(currentShoppingListsItem));
            }
        }

        public void DeleteShoppingListsItem(ShoppingListsItem shoppingListsItem)
        {
            if ((shoppingListsItem.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(shoppingListsItem);
            }
            this.ObjectContext.DeleteObject(shoppingListsItem);
        }

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<Store> GetStores()
        {
            return this.ObjectContext.Stores.OrderBy(e=>e.Name);
        }

        public void InsertStore(Store store)
        {
            if ((store.EntityState != EntityState.Added))
            {
                if ((store.EntityState != EntityState.Detached))
                {
                    this.ObjectContext.ObjectStateManager.ChangeObjectState(store, EntityState.Added);
                }
                else
                {
                    this.ObjectContext.AddToStores(store);
                }
            }
        }

        public void UpdateStore(Store currentStore)
        {
            if ((currentStore.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentStore, this.ChangeSet.GetOriginal(currentStore));
            }
        }

        public void DeleteStore(Store store)
        {
            if ((store.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(store);
            }
            this.ObjectContext.DeleteObject(store);
        }

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<StoresLocation> GetStoresLocations()
        {
            return this.ObjectContext.StoresLocations;
        }

        public void InsertStoresLocation(StoresLocation storesLocation)
        {
            if ((storesLocation.EntityState != EntityState.Added))
            {
                if ((storesLocation.EntityState != EntityState.Detached))
                {
                    this.ObjectContext.ObjectStateManager.ChangeObjectState(storesLocation, EntityState.Added);
                }
                else
                {
                    this.ObjectContext.AddToStoresLocations(storesLocation);
                }
            }
        }

        public void UpdateStoresLocation(StoresLocation currentStoresLocation)
        {
            if ((currentStoresLocation.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentStoresLocation, this.ChangeSet.GetOriginal(currentStoresLocation));
            }
        }

        public void DeleteStoresLocation(StoresLocation storesLocation)
        {
            if ((storesLocation.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(storesLocation);
            }
            this.ObjectContext.DeleteObject(storesLocation);
        }

        // TODO: Consider
        // 1. Adding parameters to this method and constraining returned results, and/or
        // 2. Adding query methods taking different parameters.
        public IQueryable<Supplier> GetSuppliers()
        {
            return this.ObjectContext.Suppliers.OrderBy(e=>e.Name);
        }

        public void InsertSupplier(Supplier supplier)
        {
            if ((supplier.EntityState != EntityState.Added))
            {
                if ((supplier.EntityState != EntityState.Detached))
                {
                    this.ObjectContext.ObjectStateManager.ChangeObjectState(supplier, EntityState.Added);
                }
                else
                {
                    this.ObjectContext.AddToSuppliers(supplier);
                }
            }
        }

        public void UpdateSupplier(Supplier currentSupplier)
        {
            if ((currentSupplier.EntityState == EntityState.Detached))
            {
                this.ObjectContext.AttachAsModified(currentSupplier, this.ChangeSet.GetOriginal(currentSupplier));
            }
        }

        public void DeleteSupplier(Supplier supplier)
        {
            if ((supplier.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Attach(supplier);
            }
            this.ObjectContext.DeleteObject(supplier);
        }
    }
}


